﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace STAThreadDemo
{
    /// <summary>
    /// 自定义类，测试线程方法
    /// </summary>
    public class Apalser
    {
        /// <summary>
        /// 线程关联的Beat()方法
        /// </summary>
        public void Beat()
        {
            int sumTemp = 0;
            while (true)
            {
                Console.WriteLine("Apalser.Beat is  runing  in its  own thread with 10 times.");
                sumTemp++;
                if (sumTemp == 10)
                {
                    break;
                }
            }
        }
    }

    public class Simple
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Thread   Start/Stop/Join     Simple");
            Apalser myApalser = new Apalser();
            //创建一个线程，便于执行Apalser类的Beat()
            Thread myThread = new Thread(new ThreadStart(myApalser.Beat));
            myThread.Start();
            while (!myThread.IsAlive)
            {
                Thread.Sleep(2000);//线程挂起1毫秒
                myThread.Abort();
                myThread.Join();//使主线程等待，直到myThread线程结束
                Console.WriteLine();
                Console.WriteLine("Apalser.Beat     has     finished !");
                //try
                //{
                //    Console.WriteLine("try  to  restart Apalser.Beat  Thread ");
                //    myThread.Start();
                //}
                //catch (ThreadStateException)
                //{
                //    Console.WriteLine("ThreadStateException  trying  to  restart  Apalser.Beat . ");
                //    Console.Write("Expected since aborted threads cannot be restarted.");
                //    Console.ReadKey();
                //}
                Console.ReadKey();
                // return ;
            }
            Console.ReadKey();
        }

        /// <summary>
        /// 首先大家肯定知道在Console程序中，主线程自上而下着运行着main函数，假如我们在main函数中新增一个线程thread对象的话，
        /// 也就是说，在主线程中再开启一个子线程，同时子线程和主线程可以同时工作（前提是子线程使用Start方法），同理，假如我在这
        /// 个子线程中再开辟一个属于这个子线程的子线程，同理这3个爷爷，父亲，儿子线程也可以使用Start()方法一起工作，假如在主线
        /// 程中添加2个thread对象并开启，那么这2 线程便属于同一层次的线程（兄弟线程）（和优先级无关,只同一位置层次上的兄弟）
        /// </summary>
        /// <param name="grandFatherThread"></param>
        public static void ShowFatherAndSonThread(Thread grandFatherThread)
        {
            Thread sdfsd = new Thread(
            Console.WriteLine("爷爷主线程名称:{0}  ", grandFatherThread.Name);
            Thread brotherThread = new Thread(new ThreadStart(
                        () =>
                        {
                            Console.WriteLine("兄弟线程名称:{0}   ", Thread.CurrentThread.Name);
                        }
                    ));
            Thread fatherThread = new Thread(new ThreadStart(
                () =>
                {
                    Console.WriteLine("父亲线程名称:{0}   ", Thread.CurrentThread.Name);
                    Thread sonThread = new Thread(new ThreadStart(
                        () => 
                        { 
                            Console.WriteLine("儿子线程名称:{0}  ", Thread.CurrentThread.Name);
                        }
                        ));
                    sonThread.Name = "SonThread";
                    sonThread.Start();
                }
            ));
            fatherThread.Name = "FatherThread";
            brotherThread.Name = "BrotherThread";
            fatherThread.Start();
            brotherThread.Start();
        }

    }
}
